package com.bonuo.erp.service.impl;

import com.bonuo.erp.domain.EmployeeRoster;
import com.bonuo.erp.mapper.EmployeeRosterMapper;
import com.bonuo.erp.service.EmployeeRosterService;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
public class EmployeeRosterServiceImpl implements EmployeeRosterService {

    Logger log = LoggerFactory.getLogger(EmployeeRosterServiceImpl.class);

    @Autowired
    private EmployeeRosterMapper employeeMapper;


    @Override
    public List<EmployeeRoster> selectEmployeeRosterList(EmployeeRoster employee) {
        return employeeMapper.selectEmployeeRosterList(employee);
    }

    @Override
    public Integer insertEmployeeRoster(EmployeeRoster employee) {
        return employeeMapper.insertEmployeeRoster(employee);
    }

    @Override
    public Integer updateEmployeeRoster(EmployeeRoster employee) {
        return employeeMapper.updateEmployeeRoster(employee);
    }

    @Override
    public String importEmployee(List<EmployeeRoster> employeeList, boolean updateSupport, String username) {
        if (StringUtils.isNull(employeeList) || employeeList.size() == 0) {
            throw new BusinessException("导入员工数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (EmployeeRoster employee : employeeList) {
            try {
                // 验证是否存在这个员工
                EmployeeRoster oldEmp =  employeeMapper.selectEmployeeRosterByNo(employee.getEmployeeNo());

                if (oldEmp == null) {
                    employee.setCreateBy(username);
                    employee.setCreateTime(new Date());
                    insertEmployeeRoster(employee);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、工号 " + employee.getEmployeeNo() + " 导入成功");
                } else if (updateSupport) {
                    employee.setId(oldEmp.getId());
                    employee.setUpdateBy(username);
                    updateEmployeeRoster(employee);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、工号 " + employee.getEmployeeNo() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、工号 " + employee.getEmployeeNo() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、工号 " + employee.getEmployeeNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public Integer deleteEmployeeRosterById(Integer id) {
        return employeeMapper.deleteEmployeeRosterById(id);
    }
}